package com.solution.wx.my.controller;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.solution.admin.customer.entity.CompanyEntity;
import com.solution.admin.customer.service.CompanyService;
import com.solution.common.constants.PublicStatus;
import com.solution.common.controller.BaseController;
import com.solution.common.utils.string.StringUtil;

/**
 * 公司信息控制层
 * @author: solin
 * @date: 2017年11月22日 下午2:26:36
 */
@RequestMapping("/wx/my/companyInfo")
@Controller("wxCompanyInfoController")
public class CompanyInfoController extends BaseController{
	private Logger logger = LoggerFactory.getLogger(CompanyInfoController.class);
	@Autowired
	private CompanyService companyService;
	
	/**
	 * 跳转到公司信息页面
	 * @param request
	 * @return
	 * @author: solin
	 * @date: 2017年11月24日 下午2:34:35
	 */
	@RequestMapping("")
	public String toCompanyInfoPage(HttpServletRequest request) {
		Long companyId = this.getCompanyId();
		CompanyEntity companyInfo = companyService.getCompanyById(companyId);
		request.setAttribute("companyInfo", companyInfo);
		return "/wx/my/companyInfo/companyInfo";
	}
	
	/**
	 * 跳转到修改公司名称页面
	 * @param request
	 * @return
	 * @author: solin
	 * @date: 2017年11月24日 下午2:34:42
	 */
	@RequestMapping("toCompanyNamePage")
	public String toCompanyNamePage(HttpServletRequest request) {
		Long companyId = this.getCompanyId();
		CompanyEntity companyInfo = companyService.getCompanyById(companyId);
		request.setAttribute("companyName", companyInfo.getCompanyName());
		return "/wx/my/companyInfo/company_form_companyName";
	}
	
	/**
	 * 跳转到修改公司电话页面
	 * @param request
	 * @return
	 * @author: solin
	 * @date: 2017年11月24日 下午2:34:42
	 */
	@RequestMapping("toTelPage")
	public String toTelPage(HttpServletRequest request) {
		Long companyId = this.getCompanyId();
		CompanyEntity companyInfo = companyService.getCompanyById(companyId);
		request.setAttribute("tel", companyInfo.getTel());
		return "/wx/my/companyInfo/company_form_tel";
	}
	
	/**
	 * 跳转到修改税号页面
	 * @param request
	 * @return
	 * @author: solin
	 * @date: 2017年11月24日 下午2:34:42
	 */
	@RequestMapping("toTaxNoPage")
	public String toTaxNoPage(HttpServletRequest request) {
		Long companyId = this.getCompanyId();
		CompanyEntity companyInfo = companyService.getCompanyById(companyId);
		request.setAttribute("taxNo", companyInfo.getTaxNo());
		return "/wx/my/companyInfo/company_form_taxNo";
	}
	
	/**
	 * 跳转到修改开户行页面
	 * @param request
	 * @return
	 * @author: solin
	 * @date: 2017年11月24日 下午2:34:42
	 */
	@RequestMapping("toBankNamePage")
	public String toBankNamePage(HttpServletRequest request) {
		Long companyId = this.getCompanyId();
		CompanyEntity companyInfo = companyService.getCompanyById(companyId);
		request.setAttribute("bankName", companyInfo.getBankName());
		return "/wx/my/companyInfo/company_form_bankName";
	}
	
	/**
	 * 跳转到修改银行账号页面
	 * @param request
	 * @return
	 * @author: solin
	 * @date: 2017年11月24日 下午2:34:42
	 */
	@RequestMapping("toBankAccountPage")
	public String toBankAccountPage(HttpServletRequest request, String bankAccount) {
		request.setAttribute("companyName", bankAccount);
		return "/wx/my/companyInfo/company_form_bankAccount";
	}
	
	/**
	 * 更新公司信息
	 * @param request
	 * @return
	 * @author: solin
	 * @date: 2017年11月24日 下午4:32:49
	 */
	@RequestMapping("updateCompanyInfo")
	public String updateCompanyInfo(HttpServletRequest request) {
		int res =  0;
		//公司名称
		String companyName = request.getParameter("companyName");
		//公司电话
		String tel = request.getParameter("tel");
		//税号
		String taxNo = request.getParameter("taxNo");
		//开户行
		String bankName = request.getParameter("bankName");
		//银行账号
		String bankAccount = request.getParameter("bankAccount");
		try {
			Long companyId = this.getCompanyId();
			CompanyEntity companyEntity = new CompanyEntity();
			companyEntity.setId(companyId);
			if (StringUtil.isNotEmpty(companyName)) {
				companyEntity.setCompanyName(companyName);
			}
			if (StringUtil.isNotEmpty(tel)) {
				companyEntity.setTel(tel);
			}
			if (StringUtil.isNotEmpty(taxNo)) {
				companyEntity.setTaxNo(taxNo);
			}
			if (StringUtil.isNotEmpty(bankName)) {
				companyEntity.setBankName(bankName);
			}
			if (StringUtil.isNotEmpty(bankAccount)) {
				companyEntity.setBankAccount(bankAccount);
			}
			
			//更新公司信息
			res = companyService.updateCompany(companyEntity);
			if (res != 1) { 
				request.setAttribute(PublicStatus.RESULT_CODE, 0);
				request.setAttribute(PublicStatus.RESULT_MSG,  "更新公司信息失败");
			}
		} catch (Exception e) {
			request.setAttribute(PublicStatus.RESULT_CODE, 0);
			request.setAttribute(PublicStatus.RESULT_MSG,  "更新公司信息失败");
			logger.error("更新公司信息失败:{}", e);
		}
		
		if (res < 1) {
			if (StringUtil.isNotEmpty(companyName)) {
				return "/wx/my/companyInfo/company_form_companyName";
			} else if (StringUtil.isNotEmpty(tel)) {
				return "/wx/my/companyInfo/company_form_tel";
			} else if (StringUtil.isNotEmpty(taxNo)) {
				return "/wx/my/companyInfo/company_form_taxNo";
			} else if (StringUtil.isNotEmpty(bankName)) {
				return "/wx/my/companyInfo/company_form_bankName";
			} else if (StringUtil.isNotEmpty(bankAccount)) {
				return "/wx/my/companyInfo/company_form_bankAccount";
			}
		}
		
		return "redirect:/wx/my/companyInfo";
	}
}
